import java.io.IOException;
import java.util.ArrayList;

public class Printshuzu {
        public static ArrayList<Student> reader() throws IOException {
            String studentMath = Reader.openFile("http://139.186.26.196/javaweb/data/math.txt");
            String studentChinese = Reader.openFile("http://139.186.26.196/javaweb/data/chinese.txt");
            String studentEnglish = Reader.openFile("http://139.186.26.196/javaweb/data/english.txt");
            ArrayList<Student> students = StudentManager.studentArrayList();
            ArrayList<Student> studentGrades=new ArrayList<>();
            ArrayList<Student> studentsMainFinnal = new ArrayList<>();
            ArrayList<Student> studentsMathGrades = new ArrayList<>();
            ArrayList<Student> studentsChineseGrades = new ArrayList<>();
            ArrayList<Student> studentsEnglishGrades = new ArrayList<>();
            String[] stuM = studentMath.split("\n");
            String[] stuC = studentChinese.split("\n");
            String[] stuE = studentEnglish.split("\n");

            for (String s : stuM) {
                int index = s.indexOf("\t");
                Student stu = new Student();
                stu.setId(s.substring(0, index + 1));
                stu.setMath(s.substring(index + 1));
                studentsMathGrades.add(stu);
            }

            for (String s : stuC) {
                int index = s.indexOf("\t");
                Student stu = new Student();
                stu.setId(s.substring(0, index + 1));
                stu.setChinese(s.substring(index + 1));
                studentsChineseGrades.add(stu);
            }

            for (String s : stuE) {
                int index = s.indexOf("\t");
                Student stu = new Student();
                stu.setId(s.substring(0, index + 1));
                stu.setEnglish(s.substring(index + 1));
                studentsEnglishGrades.add(stu);
            }

            //ArrayList<Student>mergedList= Stream.of(studentsMathGrades,studentsChineseGrades,studentsEnglishGrades).flatMap(t -> Collection.stream(t)).collect(Collectors.toList());
            for (Student temp1 : students) {
                for (Student temp2 : studentGrades) {
                    if (temp2.getId().equals(temp1.getId())) {
                        temp1.setMath(temp2.getMath());
                        studentsMainFinnal.add(temp1);
                    } else if (temp2.getId().equals(temp1.getId())) {
                        temp1.setChinese(temp2.getChinese());
                        studentsMainFinnal.add(temp1);
                    } else if (temp2.getId().equals(temp1.getId())) {
                        temp1.setEnglish(temp2.getEnglish());
                        studentsMainFinnal.add(temp1);
                    }
                    break;
                }
            }
            return studentsMainFinnal;

        }
    }


